/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.uoc.tdp.server.impl;

import edu.uoc.tdp.common.EIssueException;
import edu.uoc.tdp.common.EIssueException;
import edu.uoc.tdp.common.SubsystemMaintenance;
import edu.uoc.tdp.dal.*;
import edu.uoc.tdp.entity.*;
import edu.uoc.tdp.i18n.*;
import java.io.Serializable;
import java.rmi.RemoteException;
import java.util.List;

/**
 * @author fjaumot
 * Per a que funcioni s'ha de fer la gestió de excepcions dins del métodes.
 */
public class SubsystemMaintenanceImpl extends java.rmi.server.UnicastRemoteObject
        implements SubsystemMaintenance, Serializable {

    private ManagementDb mngDB;

    /**
     * Consutructor por defecto
     * @throws RemoteException 
     */
    public SubsystemMaintenanceImpl() throws RemoteException, EIssueException {
        super();
    }

    public String test() throws RemoteException {
        System.out.println("test()");
        return new String("test Gestor Manteniment with RMI!");
    }
    
    ///////////////////////////////////////////////////////////////////////////////////////////////
    //
    //  USUARIS / Login 
    //
    ///////////////////////////////////////////////////////////////////////////////////////////////    

    /**
     * USERS - Login
     * @param email
     * @param password
     * @return
     * @throws RemoteException
     * @throws EIssueException 
     
    public User Login(String email, String password) throws RemoteException, EIssueException {
        try {
            ManagementUsers mngRq = new ManagementUsers();
            return mngRq.Login(email, password);
        } catch (Exception e) {
            throw new EIssueException(e.getMessage());
        }
    }*/

    ///////////////////////////////////////////////////////////////////////////////////////////////
    //
    //  USUARIS / Subsistema de Manteniment
    //
    ///////////////////////////////////////////////////////////////////////////////////////////////
    /**
     * USERS - Obtenir tots els usuaris
     * @return Llista de espais
     * @throws RemoteException
     * @throws EIssueException 
     */
    public List<User> GetAllUser() throws RemoteException, EIssueException {
        try {
            //ManagementResource mngRq = new ManagementResource();
            ManagementUsers mngRq = new ManagementUsers();
            return mngRq.GetAllUser();
        } catch (Exception e) {
            throw new EIssueException(e.getMessage());
        }
    }

    /**
     * USERS - Obtenir tots els usuaris, nomes els camps que estan al jtable de usuaris
     * @return Llista de usuaris 
     * @throws RemoteException
     * @throws EIssueException 
     */
    public List<User> GetAllUserTable() throws RemoteException, EIssueException {
        try {
            //ManagementResource mngRq = new ManagementResource();
            ManagementUsers mngRq = new ManagementUsers();
            return mngRq.GetAllUserTable();
        } catch (Exception e) {
            throw new EIssueException(e.getMessage());
        }
    }

    /**
     * USERS  - Obtenir 1 usuari  (tots els camps de la taula)
     * @param userId
     * @return Llista amb 1 usuari
     * @throws RemoteException
     * @throws EIssueException 
     */
    public List<User> GetUserById(String userId) throws RemoteException, EIssueException {
        try {
            ManagementUsers mngRq = new ManagementUsers();
            return mngRq.GetUserById(userId);
        } catch (Exception e) {
            throw new EIssueException(e.getMessage());
        }
    }

    /**
     * USERS - Comprovem si existeix el usuari abans de fer Insert 
     * @param UserId
     * @return True=Existeis el registre a la taula de usuari
     * @throws RemoteException
     * @throws EIssueException 
     */
    public boolean CheckExistsUser(String UserId) throws RemoteException, EIssueException {
        try {
            ManagementUsers mngRq = new ManagementUsers();
            return mngRq.CheckExistsUser(UserId);
        } catch (Exception e) {
            throw new EIssueException(e.getMessage());
        }
    }

    /**
     * USERS  - Obtenir usuaris filtrats per la jtable de espais (no tots els camps de la taula)
     * @param userId
     * @return Llista de usuaris
     * @throws RemoteException
     * @throws EIssueException 
     */
    public List<User> GetUse_FindById(String userId) throws RemoteException, EIssueException {
        try {
            ManagementUsers mngRq = new ManagementUsers();
            return mngRq.GetUse_FindById(userId);
        } catch (Exception e) {
            throw new EIssueException(e.getMessage());
        }
    }

    /**
     * USERS  - Obtenir usuaris filtrats per la jtable de espais (no tots els camps de la taula)
     * @param PostalCode
     * @return Llista de usuaris
     * @throws RemoteException
     * @throws EIssueException 
     */
    public List<User> GetUse_FindByPostalCode(Integer PostalCode) throws RemoteException, EIssueException {
        try {
            ManagementUsers mngRq = new ManagementUsers();
            return mngRq.GetUse_FindByPostalCode(PostalCode);
        } catch (Exception e) {
            throw new EIssueException(e.getMessage());
        }
    }

    /**
     * USERS  - Obtenir usuaris filtrats per la jtable de espais (no tots els camps de la taula)
     * @param userName
     * @return Llista de usuaris
     * @throws RemoteException
     * @throws EIssueException 
     */
    public List<User> GetUse_FindByName(String userId) throws RemoteException, EIssueException {
        try {
            ManagementUsers mngRq = new ManagementUsers();
            return mngRq.GetUse_FindByName(userId);
        } catch (Exception e) {
            throw new EIssueException(e.getMessage());
        }
    }

    /**
     * USERS  - Obtenir usuaris filtrats per la jtable de espais (no tots els camps de la taula)
     * @param Company
     * @return Llista de usuaris
     * @throws RemoteException
     * @throws EIssueException 
     */
    public List<User> GetUse_FindByCompany(String Company) throws RemoteException, EIssueException {
        try {
            ManagementUsers mngRq = new ManagementUsers();
            return mngRq.GetUse_FindByCompany(Company);
        } catch (Exception e) {
            throw new EIssueException(e.getMessage());
        }
    }

    /**
     * USERS - Nou usuari / Insert a la taula de usuaris
     * @param User
     * @return boolean, True=Inserció Ok
     * @throws RemoteException
     * @throws EIssueException 
     */
    public boolean SaveUser(User User) throws RemoteException, EIssueException {
        try {
            ManagementUsers mngRq = new ManagementUsers();
            return mngRq.SaveUser(User);
        } catch (Exception e) {
            throw new EIssueException(e.getMessage());
        }
    }

    /**
     * USERS - Guardar canvis / Update a la taula de usuaris
     * @param User
     * @return boolean, True=Inserció Ok
     * @throws RemoteException
     * @throws EIssueException 
     */
    public boolean SaveUserChanges(User User) throws RemoteException, EIssueException {
        try {
            ManagementUsers mngRq = new ManagementUsers();
            return mngRq.SaveUserChanges(User);
        } catch (Exception e) {
            throw new EIssueException(e.getMessage());
        }
    }

    /**
     * USERS - Canviar password / Update a la taula de usuaris
     * @param UserId
     * @param Passw
     * @return Boolea True-update ok
     * @throws RemoteException
     * @throws EIssueException 
     */
    public boolean SavePassword(String UserId, String Passw) throws RemoteException, EIssueException {
        try {
            ManagementUsers mngRq = new ManagementUsers();
            return mngRq.SavePassword(UserId, Passw);
        } catch (Exception e) {
            throw new EIssueException(e.getMessage());
        }
    }

    /**
     * USERS - Donar de baixa un usuari / Update a la taula de usuaris
     * @param InactivatUser
     * @param userId
     * @return
     * @throws RemoteException
     * @throws EIssueException 
     */
    public boolean SaveInactivationUser(String InactivatUser, String userId) throws RemoteException, EIssueException {
        try {
            ManagementUsers mngRq = new ManagementUsers();
            return mngRq.SaveInactivationUser(InactivatUser, userId);
        } catch (Exception e) {
            throw new EIssueException(e.getMessage());
        }
    }

    /**
     * Obtenim llista de Rols per al combo de la pantalla usuaris
     * @return llista de Rols 
     * @throws RemoteException
     * @throws EIssueException 
     */
    public List<Rol> GetAllRoles() throws RemoteException, EIssueException {
        try {
            ManagementUsers mngRq = new ManagementUsers();
            return mngRq.GetAllRoles();
        } catch (Exception e) {
            throw new EIssueException(e.getMessage());
        }
    }

    ///////////////////////////////////////////////////////////////////////////////////////////////
    //
    //  ESPAIS / Subsistema de Manteniment
    //
    ///////////////////////////////////////////////////////////////////////////////////////////////
    /**
     * SPACES - Obtenir tots els espais
     * @return Llista de espais
     * @throws RemoteException
     * @throws EIssueException 
     */
    public List<Space> GetAllSpaces() throws RemoteException, EIssueException {
        try {
            //ManagementResource mngRq = new ManagementResource();
            ManagementSpaces mngRq = new ManagementSpaces();
            return mngRq.GetAllSpaces();
        } catch (Exception e) {
            throw new EIssueException(e.getMessage());
        }
    }

    /**
     * SPACES - Obtenir tots els espais per la jtable de spaces de PanelResource
     * @return Llista de espais
     * @throws RemoteException
     * @throws EIssueException 
     */
    public List<Space> GetAllSpacesTable() throws RemoteException, EIssueException {
        try {
            ManagementSpaces mngRq = new ManagementSpaces();
            return mngRq.GetAllSpacesTable();
        } catch (Exception e) {
            throw new EIssueException(e.getMessage());
        }
    }

    /**
     * SPACES  - Obtenir 1 espais 
     * @param IdResource
     * @return Llista amb 1 espai
     * @throws RemoteException
     * @throws EIssueException 
     */
    public List<Space> GetOneSpace(int SpaceId) throws RemoteException, EIssueException {
        try {
            ManagementSpaces mngRq = new ManagementSpaces();
            return mngRq.GetOneSpace(SpaceId);
        } catch (Exception e) {
            throw new EIssueException(e.getMessage());
        }
    }

    /**
     * SPACES - Comprovem si existeix el espai abans de fer Insert 
     * @param SpaceId
     * @return True=Existeis el registre a la taula de espais
     * @throws RemoteException
     * @throws EIssueException 
     */
    public boolean CheckExistsSpace(int SpaceId) throws RemoteException, EIssueException {
        try {
            ManagementSpaces mngRq = new ManagementSpaces();
            return mngRq.CheckExistsSpace(SpaceId);
        } catch (Exception e) {
            throw new EIssueException(e.getMessage());
        }
    }

    /**
     * SPACES - Obtenir espais filtrats per la jtable de espais (no tots els camps de la taula)
     * @param Desc Where spacecedescription like %param%
     * @return Llista de espais
     * @throws RemoteException
     * @throws EIssueException 
     */
    public List<Space> GetSpace_FindByDesc(String Desc) throws RemoteException, EIssueException {
        try {
            ManagementSpaces mngRq = new ManagementSpaces();
            return mngRq.GetSpace_FindByDesc(Desc);
        } catch (Exception e) {
            throw new EIssueException(e.getMessage());
        }
    }

    /**
     * SPACES - Obtenir espais filtrats per la jtable de espais
     * @param PostalCode 
     * @return
     * @throws RemoteException
     * @throws EIssueException 
     */
    public List<Space> GetSpace_FindByPostalCode(Integer PostalCode) throws RemoteException, EIssueException {
        try {
            ManagementSpaces mngRq = new ManagementSpaces();
            return mngRq.GetSpace_FindByPostalCode(PostalCode);
        } catch (Exception e) {
            throw new EIssueException(e.getMessage());
        }
    }

    /**
     * SPACES - Insert a la taula de espais
     * @param Space
     * @return boolean, True=Inserció Ok
     * @throws RemoteException
     * @throws EIssueException 
     */
    public boolean SaveSpace(Space Space) throws RemoteException, EIssueException {
        try {
            ManagementSpaces mngRq = new ManagementSpaces();
            return mngRq.SaveSpace(Space);
        } catch (Exception e) {
            throw new EIssueException(e.getMessage());
        }
    }

    /**
     * SPACES - Guardar canvis a un espai ja existent / Update a la taula de espais
     * @param Res
     * @return boolean, True=Update Ok
     * @throws RemoteException
     * @throws EIssueException 
     */
    public boolean SaveSpaceChanges(Space Space) throws RemoteException, EIssueException {
        try {
            ManagementSpaces mngRq = new ManagementSpaces();
            return mngRq.SaveSpaceChanges(Space);
        } catch (Exception e) {
            throw new EIssueException(e.getMessage());
        }
    }

    /**
     * SPACES - Donar de baixa un espai / Update a la taula de espais
     * @param spaceId
     * @param userName
     * @return boolean, True=Update Ok
     * @throws RemoteException
     * @throws EIssueException 
     */
    public boolean SaveInactivationSpace(int spaceId, String userName) throws RemoteException, EIssueException {
        try {
            ManagementSpaces mngRq = new ManagementSpaces();
            return mngRq.SaveInactivationSpace(spaceId, userName);
        } catch (Exception e) {
            throw new EIssueException(e.getMessage());
        }
    }

    ///////////////////////////////////////////////////////////////////////////////////////////////
    //
    //  RECURSOS / Subsistema de Manteniment
    //
    ///////////////////////////////////////////////////////////////////////////////////////////////    
    /**
     * RESOURCES - Obtenir tots els recursos
     * @return
     * @throws RemoteException
     * @throws EIssueException 
     */
    public List<Resource> GetAllResources() throws RemoteException, EIssueException {
        try {
            //ManagementResource mngRq = new ManagementResource();
            ManagementResource mngRq = new ManagementResource();
            return mngRq.GetAllResources();
        } catch (Exception e) {
            throw new EIssueException(e.getMessage());
        }
    }

    /**
     * RESOURCES - Obtenir tots els recursos per la jtable de recursos de PanelResource
     * @return
     * @throws RemoteException
     * @throws EIssueException 
     */
    public List<Resource> GetAllResourcesTable() throws RemoteException, EIssueException {
        try {
            ManagementResource mngRq = new ManagementResource();
            return mngRq.GetAllResourcesTable();
        } catch (Exception e) {
            throw new EIssueException(e.getMessage());
        }
    }

    /**
     * RESOURCES - Obtenir 1 recurs 
     * @param IdResource
     * @return
     * @throws RemoteException
     * @throws EIssueException 
     */
    public List<Resource> GetOneResource(int resourceid) throws RemoteException,EIssueException{
        try {
            ManagementResource mngRq = new ManagementResource();
            return mngRq.GetOneResource(resourceid);
        } catch (Exception e) {
            throw new EIssueException(e.getMessage());
        }
    }

    /**
     * RESOURCES - Comprovem si existeix el recurs abans de fer Insert 
     * @param IdResource
     * @return True=Existeis el registre a la taula de recursos
     * @throws RemoteException
     * @throws EIssueException 
     */
    public boolean CheckExistsResource(int resourceid) throws RemoteException, EIssueException {
        try {
            ManagementResource mngRq = new ManagementResource();
            return mngRq.CheckExistsResource(resourceid);
        } catch (Exception e) {
            throw new EIssueException(e.getMessage());
        }
    }

    /**
     * RESOURCES - Obtenir recursos filtrats per la jtable de recursos de PanelResource
     * @param DescResource Where resourcedescription like %param%
     * @return Llista de Recursos
     * @throws RemoteException
     * @throws EIssueException 
     */
    public List<Resource> GetResource_FindByDesc(String DescResource) throws RemoteException, EIssueException {
        try {
            ManagementResource mngRq = new ManagementResource();
            return mngRq.GetResource_FindByDesc(DescResource);
        } catch (Exception e) {
            throw new EIssueException(e.getMessage());
        }
    }

    /**
     * RESOURCES - Guardar un nou recurs / Obtenir recursos filtrats per la jtable de recursos de PanelResource
     * @param IdResource Where resourceid = param
     * @return
     * @throws RemoteException
     * @throws EIssueException 
     */
    public List<Resource> GetResource_FindByResourceid(Integer IdResource) throws RemoteException, EIssueException {
        try {
            ManagementResource mngRq = new ManagementResource();
            return mngRq.GetResource_FindByResourceid(IdResource);
        } catch (Exception e) {
            throw new EIssueException(e.getMessage());
        }
    }

    /**
     * RESOURCES - Insert a la taula de recursos
     * @param Res
     * @return boolean, True=Inserció Ok
     * @throws RemoteException
     * @throws EIssueException 
     */
    public boolean SaveResource(Resource Res) throws RemoteException, EIssueException {
        try {
            ManagementResource mngRq = new ManagementResource();
            return mngRq.SaveResource(Res);
        } catch (Exception e) {
            throw new EIssueException(e.getMessage());
        }
    }

    /**
     * RESOURCES - Guardar canvis a un recurs ja existent / Update a la taula de recursos
     * @param Res
     * @return boolean, True=Update Ok
     * @throws RemoteException
     * @throws EIssueException 
     */
    public boolean SaveResourceChanges(Resource Res) throws RemoteException, EIssueException {
        try {
            ManagementResource mngRq = new ManagementResource();
            return mngRq.SaveResourceChanges(Res);
        } catch (Exception e) {
            throw new EIssueException(e.getMessage());
        }
    }

    /**
     * RESOURCES - Donar de baixa un recurs / Update a la taula de recursos
     * @param resourceId
     * @param userName
     * @return boolean, True=Update Ok
     * @throws RemoteException
     * @throws EIssueException 
     */
    public boolean SaveInactivationResource(int resourceId, String userName) throws RemoteException, EIssueException {
        try {
            ManagementResource mngRq = new ManagementResource();
            return mngRq.SaveInactivationResource(resourceId, userName);
        } catch (Exception e) {
            throw new EIssueException(e.getMessage());
        }
    }
    
    
    ///////////////////////////////////////////////////////////////////////////////////////////////
    //
    //  
    //
    ///////////////////////////////////////////////////////////////////////////////////////////////    

    public List<Resource> GetAllResourcesFromSpace(Integer spaceid) throws RemoteException, EIssueException {
            try {
            ManagementSpacesResources mngRq = new ManagementSpacesResources();
            return mngRq.GetAllResourcesFromSpace(spaceid);
        } catch (Exception e) {
            throw new EIssueException(e.getMessage());
        }
    }

    public boolean SaveSpaceResource(int SpaceId, int ResourceId) throws RemoteException,EIssueException{            
        try {
            ManagementSpacesResources mngRq = new ManagementSpacesResources();
            return mngRq.SaveSpaceResource(SpaceId,ResourceId);
        } catch (Exception e) {
            throw new EIssueException(e.getMessage());
        }            
    }

    
    public boolean DeleteSpaceResource(int spaceid, int resourceId) throws RemoteException,EIssueException{
        try {
            ManagementSpacesResources mngRq = new ManagementSpacesResources();
            return mngRq.DeleteSpaceResource(spaceid,resourceId);
        } catch (Exception e) {
            throw new EIssueException(e.getMessage());
        }            
    }

}
